package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.entities.ProductCategory;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Decheng Kong
 * @date 2021/11/8 15:09
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public PageVO<ProductCategoryTreeNodeVO> getCategoryTree(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        Example example = new Example(ProductCategory.class);
        example.createCriteria().andEqualTo("pid", 0);

        // 查询分页信息
        List<ProductCategory> pageInfo = productCategoryMapper.selectByExample(example);
        List<ProductCategory> all = productCategoryMapper.selectAll();


        List<ProductCategoryTreeNodeVO> nodes = all.stream()
                .map(c -> {
                    //转vo
                    ProductCategoryTreeNodeVO node = new ProductCategoryTreeNodeVO();
                    BeanUtils.copyProperties(c, node);
                    return node;
                }).collect(Collectors.toList());

        List<ProductCategoryTreeNodeVO> collect = nodes.stream()
                .filter(n -> n.getPid() == 0)
                .map(n -> {
                    n.setLev(1);
                    n.setChildren(getChildren(n, nodes));
                    return n;
                })
                .sorted(ProductCategoryTreeNodeVO.order())
                .collect(Collectors.toList());

        PageInfo<ProductCategory> info = new PageInfo<>(pageInfo);
        return new PageVO<>(info.getTotal(), collect);

    }

    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {

        List<ProductCategory> all = productCategoryMapper.selectAll();


        List<ProductCategoryTreeNodeVO> nodes = all.stream()
                .map(c -> {
                    //转vo
                    ProductCategoryTreeNodeVO node = new ProductCategoryTreeNodeVO();
                    BeanUtils.copyProperties(c, node);
                    return node;
                }).collect(Collectors.toList());

        List<ProductCategoryTreeNodeVO> collect = nodes.stream()
                .filter(n -> n.getPid() == 0)
                .map(n -> {
                    n.setLev(1);
                    n.setChildren(getChildren(n, nodes));
                    return n;
                })
                .sorted(ProductCategoryTreeNodeVO.order())
                .collect(Collectors.toList());

        return collect;
    }

    @Override
    public void addCategory(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO, productCategory);
        productCategoryMapper.insert(productCategory);
    }

    @Override
    public ProductCategoryVO getCategoryById(Integer id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        ProductCategoryVO productCategoryVO = new ProductCategoryVO();
        BeanUtils.copyProperties(productCategory, productCategoryVO);
        return productCategoryVO;
    }

    @Override
    public void updateCategory(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO, productCategory);
        productCategoryMapper.updateByPrimaryKey(productCategory);
    }

    @Override
    public void deleteCategory(Integer id) throws BusinessException {
        // 1. 有子分类的不能删除
        Example example = new Example(ProductCategory.class);
        example.createCriteria().andEqualTo("pid", id);
        int count = productCategoryMapper.selectCountByExample(example);
        if (count > 0) {
            throw new BusinessException(BusinessCodeEnum.PRODUCT_CATEGORY_DELETE_ERROR_SUB);
        }
        // TODO 2. 有物资引用的不能删除
        // 3. 删除分类
        productCategoryMapper.deleteByPrimaryKey(id);
    }

    private List<ProductCategoryTreeNodeVO> getChildren(ProductCategoryTreeNodeVO parent, List<ProductCategoryTreeNodeVO> all) {
        List<ProductCategoryTreeNodeVO> children = all.stream()
                .filter(n -> n.getPid().equals(parent.getId()))
                .map(n -> {
                    n.setLev(parent.getLev() + 1);
                    n.setChildren(getChildren(n, all));
                    return n;
                })
                .sorted(ProductCategoryTreeNodeVO.order())
                .collect(Collectors.toList());
        return children;
    }

}
